//
//  SceneEditCell.m
//  YSSmartHome
//
//  Created by yanghao on 16/9/18.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "SceneEditCell.h"
#import "DataManager.h"
#import "ZTDevice.h"


@implementation SceneEditCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        self.textLabel.font = [UIFont systemFontOfSize:15];
        self.detailTextLabel.font = [UIFont systemFontOfSize:15];
    }
    return self;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)setLinkItemDes:(XAILinkItemDes *)linkItemDes
{
    _linkItemDes = linkItemDes;
    if (linkItemDes.tlv.type == XAI_DATA_TYPE_ASCII_TEXT )
    {
        NSString *name = [[NSString alloc]initWithBytes:[linkItemDes.tlv getPayloadbyte] length:linkItemDes.tlv.payloadlen encoding:NSUTF8StringEncoding];
        NSString *title = [NSString stringWithFormat:@"当平台点击 \"%@\"时",name];
        self.textLabel.text = title;
    }
    else if (linkItemDes.tlv.type == XAI_DATA_TYPE_BIN_DATE)
    {
        self.textLabel.text = @"时间联动";
    }
    else
    {
        NSMutableArray *devices = kDataManager.deviceListenList;
        ZTDevice *dev = nil;
        for (ZTDevice *device  in devices)
        {
            if (device.apsn == linkItemDes.apsn && device.luid == linkItemDes.luid)
            {
                dev = device;
                break;
            }
        }
        if (dev)
        {
            if (dev.devType == ZTDeviceTypeOneWay0Switch ||
                dev.devType == ZTDeviceTypeOneWaySwitch ||
                dev.devType == ZTDeviceTypeTwoWay0Switch ||
                dev.devType == ZTDeviceTypeTwoWaySwitch ||
                dev.devType == ZTDeviceTypeThreeWay0Switch ||
                dev.devType == ZTDeviceTypeThreeWaySwitch ||
                dev.devType == ZTDeviceTypeFourWay0Switch ||
                dev.devType == ZTDeviceTypeFourWaySwitch ||
                dev.devType == ZTDeviceTypeOneWaySocket
                )
            {
                NSString *channle = nil;
                XAILinkItemDes *linkItemDes = self.linkItemDes;
                switch (linkItemDes.oprId) {
                    case 0x01:
                        channle = @"一路";
                        break;
                    case 0x02:
                        channle = @"二路";
                        break;
                    case 0x03:
                        channle = @"三路";
                        break;
                    case 0x04:
                        channle = @"四路";
                        break;
                        
                    default:
                        break;
                }
                self.textLabel.text = [NSString stringWithFormat:@"%@  %@", [dev getDeviceName], channle];
            }else {
                self.textLabel.text = [NSString stringWithFormat:@"%@", [dev getDeviceName]];
            }
            switch (self.linkItemDes.tlv.type) {
                case XAI_DATA_TYPE_BIN_BOOL:
                {
                    BOOL isOpen = 0;
                    memcpy(&isOpen, [self.linkItemDes.tlv getPayloadbyte], sizeof(BOOL));
                    if (dev.devType == ZTDeviceTypeAlarmkey ||
                        dev.devType == ZTDeviceTypeFlooding ||
                        dev.devType == ZTDeviceTypeGAS ||
                        dev.devType == ZTDeviceTypeAir ||
                        dev.devType == ZTDeviceTypeSmoke
                        ){
                        if (isOpen) {
                            self.detailTextLabel.text = @"触发";
                        }else
                        {
                            self.detailTextLabel.text = @"关闭";
                        }
                    }else {
                        if (isOpen) {
                            self.detailTextLabel.text = @"打开";
                        }else
                        {
                            self.detailTextLabel.text = @"关闭";
                        }
                        
                    }
                    
                    break;
                }
                default:
                    break;
            }
        }
    }
    
}
//- (void)setLinageInfo:(ZTLinkageInfo *)linageInfo {
//    _linageInfo = linageInfo;
//    if (linageInfo.datas->data_type == XAI_DATA_TYPE_ASCII_TEXT) {
//        NSString *name = [[NSString alloc]initWithBytes:linageInfo.datas->data length:linageInfo.datas->data_len encoding:NSUTF8StringEncoding];
//        NSString *title = [NSString stringWithFormat:@"当平台点击 \"%@\"时",name];
//        self.textLabel.text = title;
//    } else if (linageInfo.datas->data_type == XAI_DATA_TYPE_BIN_DATE) {
//      self.textLabel.text = @"时间联动";
//    } else {
//        NSMutableArray *devices = kDataManager.deviceListenList;
//        ZTDevice *dev = nil;
//        for (ZTDevice *device  in devices) {
//            if (device.apsn == linageInfo.dev_apsn && device.luid == linageInfo.dev_luid) {
//                dev = device;
//                break;
//            }
//        }
//        if (dev) {
//            if (dev.devType == ZTDeviceTypeOneWay0Switch ||
//                dev.devType == ZTDeviceTypeOneWaySwitch ||
//                dev.devType == ZTDeviceTypeTwoWay0Switch ||
//                dev.devType == ZTDeviceTypeTwoWaySwitch ||
//                dev.devType == ZTDeviceTypeThreeWay0Switch ||
//                dev.devType == ZTDeviceTypeThreeWaySwitch ||
//                dev.devType == ZTDeviceTypeFourWay0Switch ||
//                dev.devType == ZTDeviceTypeFourWaySwitch ||
//                dev.devType == ZTDeviceTypeOneWaySocket
//                ) {
//                
//                NSString *channle = nil;
//                ZTLinkageInfo *linkageInfo = self.linageInfo;
//                switch (linkageInfo.some_id) {
//                    case 0x01:
//                        channle = @"一路";
//                        break;
//                    case 0x02:
//                        channle = @"二路";
//                        break;
//                    case 0x03:
//                        channle = @"三路";
//                        break;
//                    case 0x04:
//                        channle = @"四路";
//                        break;
//                        
//                    default:
//                        break;
//                }
//                self.textLabel.text = [NSString stringWithFormat:@"%@  %@", [dev getDeviceName], channle];
//            }else {
//                self.textLabel.text = [NSString stringWithFormat:@"%@", [dev getDeviceName]];
//            }
//            switch (self.linageInfo.datas->data_type) {
//                case XAI_DATA_TYPE_BIN_BOOL:
//                {
//                    BOOL isOpen = 0;
//                    byte_data_copy(&isOpen, linageInfo.datas->data, sizeof(BOOL), self.linageInfo.datas->data_len);
//                    if (dev.devType == ZTDeviceTypeAlarmkey ||
//                        dev.devType == ZTDeviceTypeFlooding ||
//                        dev.devType == ZTDeviceTypeGAS ||
//                        dev.devType == ZTDeviceTypeAir ||
//                        dev.devType == ZTDeviceTypeSmoke
//                        ){
//                        if (isOpen) {
//                            self.detailTextLabel.text = @"触发";
//                        }else
//                        {
//                            self.detailTextLabel.text = @"关闭";
//                        }
//                    }else {
//                        if (isOpen) {
//                            self.detailTextLabel.text = @"打开";
//                        }else
//                        {
//                            self.detailTextLabel.text = @"关闭";
//                        }
//                        
//                    }
//                    
//                    break;
//                }
//                default:
//                    break;
//            }
//        }
//    }
//}

@end
